package cn.maxhou.mapper;

import cn.maxhou.pojo.SysUser;
import cn.maxhou.utils.MyBatisUtil;
import junit.framework.TestCase;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SysUserMapperTest2 extends TestCase {
    private Logger logger = Logger.getLogger(SysUserMapperTest2.class);

    public void update(){
        logger.debug("tetsAdd !=======================================");
        SqlSession sqlSession=null;
        int count =0;

        try{
            sqlSession = MyBatisUtil.createSqlSession();
            SysUser user = new SysUser();
            user.setId(31);
            user.setAccount("test002");
            user.setRealName("测试用户0022");
            user.setPassword("1234567");
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2003-10-22");
            user.setBirthday(birthday);
            user.setSex(1);
            user.setRoleId(1);
            user.setCreatedUserId(1);
            user.setCreatedTime(new Date());
            count=sqlSession.getMapper(SysUserMapper.class).modifY(user);

            //模拟发生异常,校验事务回滚操作
            //int i=2/0;
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            //count =0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("tetsModifY count: " + count);
    }


    public void testUpdatePwd(){
        logger.debug("testUpdatePwd !=====================================");
        SqlSession sqlSession=null;
        String pwd="888888";
        Integer id=31;
        int count=0;

        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SysUserMapper.class).updatePwd(id, pwd);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testUpdatePwd count: " + count);
    }

    @Test void deleteUser(){
        SqlSession sqlSession=null;
        int count=0;
        int id=30;
        logger.debug("testDeleteUser !=====================================");


        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SysUserMapper.class).deleteUserById(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testDeleteUser count: " + count);

    }
}
